package Classe;

import java.util.ArrayList;
import java.util.Observable;

import Classe.Sejour;

public class Facture extends Observable {
	
	private final double TPS = 0.05;
	private final double TVQ = 0.095;
	
	private double m_sousTotal;
	private double m_tps;
	private double m_tvq;
	private double m_total;
	private ArrayList<Sejour> m_sejourListe;
	
	public double getSousTotal()
	{
		return arrondir(m_sousTotal, 2);
	}
	
	public void setSousTotal()
	{
		m_sousTotal = 0;
		for (int i = 0; i < m_sejourListe.size(); i++) {
			for (int j = 0; j < m_sejourListe.get(i).getListLigneSejour().size(); j++) {
				m_sousTotal += m_sejourListe.get(i).getListLigneSejour().get(j).getMontant();
			}
		} 
	}
	
	public double getTPS()
	{
		return arrondir(m_tps, 2);
	}
	
	public void setTPS()
	{
		m_tps = m_sousTotal * TPS;
	}
	
	public double getTVQ()
	{
		return arrondir(m_tvq, 2);
	}
	
	public void setTVQ()
	{
		m_tvq = (m_sousTotal + m_tps) * TVQ;
	}
	
	public double getTotal()
	{
		return arrondir(m_total, 2);
	}
	
	public void setTotal()
	{
		m_total = m_sousTotal + m_tps + m_tvq;;
	}
	
	public ArrayList<Sejour> getListeSejour()
	{
		return m_sejourListe;
	}
	
	public Sejour getSejour(int numeroChambre)
	{
		for (Sejour s : getListeSejour()) {
			if (s.getLigneSejourCourante().getChambre().getNumeroChambre() == numeroChambre)
				return s;
		}
		
		return null;
	}
	
	public boolean peuxAjouterSejour(int numeroChambre)
	{
		for (Sejour s : getListeSejour()) {
			if (s.getLigneSejourCourante().getChambre().getNumeroChambre() == numeroChambre)
				return false;
		}
		
		return true;
	}
	
	public void ajouterSejour(Sejour s)
	{
		m_sejourListe.add(s);
		mettreAJourFacture();
		setChanged();
		notifyObservers(m_sejourListe);
	}
	
	public void retirerSejour(Sejour s)
	{
		m_sejourListe.remove(s);
		mettreAJourFacture();
		setChanged();
		notifyObservers(m_sejourListe);
	}
	
	public Facture()
	{
		m_sejourListe = new ArrayList<Sejour>();
		m_sousTotal = 0;
		m_tps = 0;
		m_tvq = 0;
		m_total = 0;
	}
	
	public void mettreAJourFacture()
	{
		setSousTotal();
		setTPS();
		setTVQ();
		setTotal();
	}
	
	public void payer()
	{
		for (Sejour s : m_sejourListe) {
			s.setEstPaye(true);
		}
	}
	
	public void getData()
	{
		mettreAJourFacture();
		setChanged();
		notifyObservers();
	}
	
	private double arrondir(double value, int decimalPlaces)
    {
        double augmentation = Math.pow(10, decimalPlaces);
        return Math.round(value * augmentation) / augmentation;
    }
}
